import json
from tools import *
# 系统配置，读取config.json文件
class SysConfig():
    def __init__(self):
        self.filename = 'config.json'
        self.tools    = Tools()

    def init(self):
        """初始化，成功返回True
        """
        if self.tools.file_exist(self.filename, True) == False:
            print(f"配置文件【{self.filename}】不存在")
            # self.tools.msg(f"系统配置文件【{self.filename}】不存在！", 'error')
            return False

        with open(self.filename, 'r') as f:
            self.config = json.load(f)

        return True
    
    def value(self, group, key, def_value = '', give_msg = False):
        """读取配置值
        """
        ret = def_value
        try:
            ret = self.config[group][key]
        except KeyError as e:
            print(f"{__name__} Error: {e};group:{group};key:{key}")
            if give_msg: self.tools.msg(f"系统配置【{self.filename}-{group}-{key}】读取错误:{e}", 'error')

        return ret
